<?php

namespace app\common\business;

use app\common\lib\Str;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Filesystem;

class File
{
    public function uploadFile($file): string
    {
        try{
            $size = 1024 * 1024 * 20;//20M
            $ext = 'jpg,jpeg,gif,png,bmp';
            validate(['file'=>['fileSize'=>$size,'fileExt'=>$ext]])->check(['file'=>$file]);

            $filePath=Filesystem::disk('public')->putFile('uploads',$file);
        } catch (ValidateException $e){
            throw new Exception($e->getMessage());
        }

        $domain = Str::globalRequestDomain();
        return $domain.'/'.str_replace(DIRECTORY_SEPARATOR,'/',$filePath);
    }
}
